﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;

namespace Sharp_Explorer
{
    public partial class SExplorer : Form
    {
        public class ofThreads
        {
            public static List<Thread> searchThreads = new List<Thread>();
        }

        public SExplorer()
        {
            InitializeComponent();
        }

        private void killThreads()
        {
            foreach (Thread t in ofThreads.searchThreads) { t.Abort(); }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            addTab();
        }

        delegate void otherNode(String newNode);
        private void netComputerNode(TreeView whichTree, string newNode)
        {
            TreeNode computer = new TreeNode();
            computer.Text = newNode;
            whichTree.Nodes.Add(computer);
        }

        public delegate void txtWriter(string file);
        public void txtSafeWrite(string content)
        {
            if (rtbPreview.InvokeRequired)
                rtbPreview.Invoke(new txtWriter(txtSafeWrite), new object[] { content });
            else
            {
                rtbPreview.Clear();
                rtbPreview.Text = content;
            }
        }
        public void addTexttoRTB(string toAdd)
        {
            txtSafeWrite(toAdd);
        }

        public static int _Unsafe_TextBoxAdd(List<string> ListTOAdd)
        {

            return 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TabPage currentTab = tabExplore.SelectedTab;
            
            addTab();
        }

        private void addTab()
        {
            
            TabPage toBuild = new TabPage();
            TabPanel newPanel = new TabPanel(this);

            newPanel.Location = new System.Drawing.Point(4, 22);
            newPanel.Padding = new System.Windows.Forms.Padding(3);
            newPanel.Size = new System.Drawing.Size(198, 421);
            newPanel.TabIndex = 0;
            newPanel.Dock = DockStyle.Fill;
            

            toBuild.Controls.Add(newPanel);
            toBuild.BackColor = Color.White;
            tabExplore.TabPages.Add(toBuild);
            

            Control[] created = newPanel.Controls.Find("TreDirectory", false);
            Control found = new TreeView();

            foreach (Control possible in created)
            {
                if (possible is System.Windows.Forms.TreeView)
                {
                    found = possible;
                }
            }
           ListDrivesInTree((TreeView)found);

        }
        
        private void ListDrivesInTree(TreeView whichTree)
        {
            String[] ofDrives = Environment.GetLogicalDrives();

            TreeNode computer = new TreeNode();
            computer.Text = "My Computer";
            whichTree.Nodes.Add(computer);

            foreach (String drive in ofDrives)
            {
                subNodeAdder(whichTree, computer, drive);

            }
        }
        
        public delegate void subNodeAdd(TreeView whichTree,TreeNode parentNode, object toAdd);
        private void subNodeAdder(TreeView whichTree,TreeNode parentNode, object toAdd)
        {


            if (whichTree.InvokeRequired)
            {
                subNodeAdd safe = new subNodeAdd(subNodeAdder);
                this.Invoke(safe, new object[] { parentNode, toAdd });
            }
            else
            {
                TreeNode thisParent = parentNode.Parent;

                parentNode.Nodes.Add(Convert.ToString(toAdd));
                parentNode.Expand();
            }

        }

        public void unpackFile(string from, string to, string ext)
        {
            FastZip fz = new FastZip();
            fz.ExtractZip(from, to, null);
        }

        private void ClearTextButton_Click(object sender, EventArgs e)
        {
            rtbPreview.Clear();
        }


    }
}
